package com.cityfood.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfProductSpecsMapper;
import com.cityfood.pojo.CfProductSpecs;
import com.cityfood.pojo.CfProductSpecsExample;
import com.cityfood.pojo.CfProductSpecsExample.Criteria;
import com.cityfood.service.ProductSpecsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ProductSpecsServiceImpl implements ProductSpecsService {
	
	@Autowired
	private CfProductSpecsMapper productSpecsmapper;

	@Override
	public List<CfProductSpecs> getProductSpecsByProductId(Integer productId) {
		CfProductSpecsExample example = new CfProductSpecsExample();
		Criteria criteria = example.createCriteria();
		criteria.andProductIdEqualTo(productId);
		List<CfProductSpecs> list = productSpecsmapper.selectByExample(example);
		if (list.size()>0) {
			return list;
		}
		return null;
	}
	
	@Override
	public PageInfo<CfProductSpecs> getSpecsByProductIdAndPage(Integer productId,Integer pageNum) {
		
		//设置分页信息
		PageHelper.startPage(pageNum, 20);
		
		CfProductSpecsExample example = new CfProductSpecsExample();

		if(productId != 0){ 
			Criteria criteria = example.createCriteria();
			criteria.andProductIdEqualTo(productId);
		}
		List<CfProductSpecs> list = productSpecsmapper.selectByExample(example);
		
		//5.获取分页信息
		PageInfo<CfProductSpecs> info = new PageInfo<>(list);
		
		return info;
	}

	@Override
	public CfProductSpecs getProductSpecsBySpecsId(Integer specsId) {
		CfProductSpecsExample example = new CfProductSpecsExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(specsId);
		criteria.andIsdisplayEqualTo((byte)1);
		List<CfProductSpecs> list = productSpecsmapper.selectByExample(example);
		CfProductSpecs specs = list.get(0);
		return specs;
	}
	
	@Override
	public int updateProductSpecsSubtract(Integer specsId, Integer number) {
		CfProductSpecs specs = this.getProductSpecsBySpecsId(specsId);
		specs.setStock(specs.getStock()-number);
		specs.setSalesVolume(specs.getSalesVolume()+number);
		specs.setUpdateTime(new Date());
		int i = this.updateProductSpecs(specs);
		return i;
	}
	
	@Override
	public int updateProductSpecsAdd(Integer specsId, Integer number) {
		CfProductSpecs specs = this.getProductSpecsBySpecsId(specsId);
		specs.setStock(specs.getStock()+number);
		specs.setSalesVolume(specs.getSalesVolume()-number);
		specs.setUpdateTime(new Date());
		int i = this.updateProductSpecs(specs);
		return i;
	}

	@Override
	public int insertProductSpecs(CfProductSpecs specs) {
		specs.setCreateTime(new Date());
		specs.setUpdateTime(new Date());
		int i = productSpecsmapper.insertSelective(specs);
		return i;
	}

	@Override
	public int updateProductSpecs(CfProductSpecs specs) {
		specs.setUpdateTime(new Date());
		int i = productSpecsmapper.updateByPrimaryKeySelective(specs);
		return i;
	}

	@Override
	public int deleteProductSpecs(CfProductSpecs specs) {
		specs.setIsdisplay((byte)0);
		specs.setUpdateTime(new Date());
		int i = productSpecsmapper.updateByPrimaryKeySelective(specs);
		return i;
	}

	@Override
	public List<CfProductSpecs> getSelectedSpecs(List<Integer> ids){
		CfProductSpecsExample example = new CfProductSpecsExample();
		Criteria criteria = example.createCriteria();

		criteria.andIdIn(ids);
        List<CfProductSpecs> list = productSpecsmapper.selectByExample(example);
        return  list;
	}

}
